/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.nhc.activity_fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import com.astrob.nhcmap.R;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.base.BaseFragment;
import zuo.biao.library.model.Entry;
import zuo.biao.library.util.StringUtil;

/**
 * 使用方法：复制>粘贴>改名>改代码
 */

/**fragment示例
 * 
 * @use new DemoFragment(),具体参考.DemoFragmentActivity(initData方法内)
 */
public class DemoFragment extends BaseFragment {
    private final String TAG = "DemoFragment";

    //与Activity通信<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    public static final String ARGUMENT_USER_ID = "ARGUMENT_USER_ID";
    public static final String ARGUMENT_USER_NAME = "ARGUMENT_USER_NAME";

    /**创建一个Fragment实例
     * @param userId
     * @return
     */
    public static DemoFragment createInstance(long userId) {
        return createInstance(userId, null);
    }

    /**创建一个Fragment实例
     * @param userId
     * @param userName
     * @return
     */
    public static DemoFragment createInstance(long userId, String userName) {
        DemoFragment fragment = new DemoFragment();

        Bundle bundle = new Bundle();
        bundle.putLong(ARGUMENT_USER_ID, userId);
        bundle.putString(ARGUMENT_USER_NAME, userName);

        fragment.setArguments(bundle);
        return fragment;
    }
    //与Activity通信>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    private long userId = 0;
    private String userName = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        //TODO demo_fragment改为你所需要的layout文件
        setContentView(R.layout.demo_fragment);

        argument = getArguments();
        if (argument != null) {
            userId = argument.getLong(ARGUMENT_USER_ID, userId);
            userName = argument.getString(ARGUMENT_USER_NAME, userName);
        }

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

        return view;//返回值必须为view
    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private ListView lvDemoFragment;

    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreateView方法内调用
        //示例代码<<<<<<<<<<<<<<
        lvDemoFragment = (ListView) findViewById(R.id.lvDemoFragment);
        //示例代码>>>>>>>>>>>>>>
    }

    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /** 示例方法 ：显示列表内容
     *
     * @param list
     */
    private void setList(List<Entry<String, String>> list) {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lvDemoFragment.setAdapter(adapter);
        }
        adapter.refresh(list);
    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private List<Entry<String, String>> list;

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreateView方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//        showShortToast(TAG + ": userId = " + userId + "; userName = " + userName);
        showProgressDialog(R.string.loading);

        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                list = getList(userId);
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        setList(list);
                    }
                });
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }


    /**示例方法：获取号码列表
     * 
     * @param userId
     * @return
     */
    protected List<Entry<String, String>> getList(long userId) {
        list = new ArrayList<Entry<String, String>>();
        for (int i = 0; i < 64; i++) {
            list.add(new Entry<String, String>("联系人" + i, String.valueOf(1311736568 + i * i)));
        }
        return list;
    }
    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreateView方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<

        lvDemoFragment.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                toActivity(UserActivity.createIntent(context, position));//一般用id，这里position仅用于测试 id));//
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>
    }
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

/** 使用方法：复制>粘贴>改名>改代码  */
    /**adapter模板，最灵活且性能最好，但如果有setOnClickListener等事件就不方便了
     * <br> 适用于ListView,GridView等AbsListView的子类
     *
     * @use new DemoAdapter(...); 具体参考.DemoActivity(setList方法内)
     */
    public class DemoAdapter extends BaseAdapter<Entry<String, String>> {
//    	private final String TAG = "DemoAdapter";

        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            //示例代码<<<<<<<<<<<<<<<<
            ViewHolder holder = convertView == null ? null : (ViewHolder) convertView.getTag();
            if (holder == null) {
                //TODO demo_item改为你所需要的layout文件
                convertView = inflater.inflate(R.layout.demo_item, parent, false);
                holder = new ViewHolder();

                holder.ivDemoItemHead = (ImageView) convertView.findViewById(R.id.ivDemoItemHead);
                holder.tvDemoItemName = (TextView) convertView.findViewById(R.id.tvDemoItemName);
                holder.tvDemoItemNumber = (TextView) convertView.findViewById(R.id.tvDemoItemNumber);

                convertView.setTag(holder);
            }

            final Entry<String, String> data = getItem(position);

            holder.tvDemoItemName.setText(StringUtil.getTrimedString(data.getValue()));
            holder.tvDemoItemNumber.setText(StringUtil.getNoBlankString(data.getKey()));

            return super.getView(position, convertView, parent);
            //示例代码>>>>>>>>>>>>>>>>
        }

        @Override
        public long getItemId(int position) {
            return getItem(position).getId();
        }
    }

    static class ViewHolder {
        //示例代码<<<<<<<<<<<<<<<<
        public ImageView ivDemoItemHead;
        public TextView tvDemoItemName;
        public TextView tvDemoItemNumber;
        //示例代码>>>>>>>>>>>>>>>>
    }


}